package org.alps.demo.annotation;

import com.sun.org.apache.bcel.internal.classfile.Annotations;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;

/**
 * @date 2021-05-31
 * @author zkw
 */
public class ParseAnnotation {

    public static void main(String[] args) {
        //1.使用类加载器加载类
        try {
            Class c = Class.forName("org.alps.demo.annotation.Child");
            //2.找到类上面的注解
            //判断类 Child 上是否存在注解：Description
            boolean isExist = c.isAnnotationPresent(Description.class);
            if (isExist) {
                //3.拿到注解实例
                Description d = (Description) c.getAnnotation(Description.class);
                System.out.println(d.value());
            }
            //4.找到方法上的注解
            Method[] ms = c.getMethods();
            for (Method m : ms) {
                boolean isM = m.isAnnotationPresent(Description.class);
                if (isM) {
                    Description d = m.getAnnotation(Description.class);
                    System.out.println(d.value());
                }
            }

            //另外一种解析方法
            for (Method m : ms) {
                Annotation[] as = m.getAnnotations();
                for (Annotation a : as) {
                    if (a instanceof Description) {
                        Description d = (Description) a;
                        System.out.println(d.value());
                    }
                }
            }

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

    }

}
